'use strict';
app.controller('printerTemplateCtrl', ['$scope','$state','toaster','$modal', '$log','utilService','printerTemplateService', function($scope,$state,toaster,$modal,$log,utilService,printerTemplateService) {
    //查询列表
    $scope.findPageAll = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'printer/template'
        }).then(function (result) {
        });
    }

    $scope.findPageAll();

    //查询小票模板信息
    $scope.del = function(id,name){
        var param = {
            id:id,
            name:name
        };

        printerTemplateService.getUseCount($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data<=0){
                        openModal("您确定要["+name+"]删除该打印机?");
                    }else{
                        alert("用打印机正在使用该样式,请清除后再进行删除");
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });




        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除打印小票样式",
                controller:"PrinterTemplateDelCtrl",
                message:msg,
                data:param
            });
        }
    }

}]);

app.controller('PrinterTemplateDelCtrl', ['$scope', '$modalInstance','$state','printerTemplateService','toaster','modalData',
    function($scope, $modalInstance,$state,printerTemplateService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.id=$scope.modal.data.id;

        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            printerTemplateService.del($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.close('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.findPageAll();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);
